{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "LqNpENf-ec0X",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "outputs": [],
      "source": [
        "!pip install -U tf-nightly"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "Pa2qpEmoVOGe",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import time\n",
        "\n",
        "import tensorflow as tf\n",
        "from tensorflow.contrib import autograph\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import six\n",
        "\n",
        "from google.colab import widgets"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "HNqUFL4deCsL",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "source": [
        "# Case study: building an RNN\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "YkC1k4HEQ7rw",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "source": [
        "In this section, we show how you can use AutoGraph to build RNNColorbot, an RNN that takes as input names of colors and predicts their corresponding RGB tuples. The model will be trained by a [custom Estimator](https://www.tensorflow.org/get_started/custom_estimators)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "7nkPDl5CTCNb",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "source": [
        "To get started, set up the dataset. The following cells defines methods that download and format the data needed for RNNColorbot; the details aren't important (read them in the privacy of your own home if you so wish), but make sure to run the cells before proceeding."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "A0uREmVXCQEw",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "outputs": [],
      "source": [
        "def parse(line):\n",
        "  \"\"\"Parses a line from the colors dataset.\"\"\"\n",
        "  items = tf.string_split([line], \",\").values\n",
        "  rgb = tf.string_to_number(items[1:], out_type=tf.float32) / 255.0\n",
        "  color_name = items[0]\n",
        "  chars = tf.one_hot(tf.decode_raw(color_name, tf.uint8), depth=256)\n",
        "  length = tf.cast(tf.shape(chars)[0], dtype=tf.int64)\n",
        "  return rgb, chars, length\n",
        "\n",
        "def load_dataset(data_dir, url, batch_size, training=True):\n",
        "  \"\"\"Loads the colors data at path into a tf.PaddedDataset.\"\"\"\n",
        "  path = tf.keras.utils.get_file(os.path.basename(url), url, cache_dir=data_dir)\n",
        "  dataset = tf.data.TextLineDataset(path)\n",
        "  dataset = dataset.skip(1)\n",
        "  dataset = dataset.map(parse)\n",
        "  dataset = dataset.cache()\n",
        "  dataset = dataset.repeat()\n",
        "  if training:\n",
        "    dataset = dataset.shuffle(buffer_size=3000)\n",
        "  dataset = dataset.padded_batch(\n",
        "      batch_size, padded_shapes=([None], [None, None], []))\n",
        "  return dataset"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "waZ89t3DTUla",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "source": [
        "To show the use of control flow, we write the RNN loop by hand, rather than using a pre-built RNN model.\n",
        "\n",
        "Note how we write the model code in Eager style, with regular `if` and `while` statements. Then, we annotate the functions with `@autograph.convert` to have them automatically compiled to run in graph mode."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "9v8AJouiC44V",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "outputs": [],
      "source": [
        "class RnnColorbot(object):\n",
        "  \"\"\"Holds the parameters of the colorbot model.\"\"\"\n",
        "\n",
        "  def __init__(self):\n",
        "    self.lower_cell = tf.contrib.rnn.LSTMBlockCell(256)\n",
        "    self.upper_cell = tf.contrib.rnn.LSTMBlockCell(128)\n",
        "    self.relu_layer = tf.layers.Dense(3, activation=tf.nn.relu)\n",
        "\n",
        "    self.lower_cell.build(tf.TensorShape((None, 256)))\n",
        "    self.upper_cell.build(tf.TensorShape((None, 256)))\n",
        "    self.relu_layer.build(tf.TensorShape((None, 128)))\n",
        "\n",
        "\n",
        "def rnn_layer(chars, cell, batch_size, training):\n",
        "  \"\"\"A simple RNN layer.\n",
        "  \n",
        "  Args:\n",
        "    chars: A Tensor of shape (max_sequence_length, batch_size, input_size)\n",
        "    cell: An object of type tf.contrib.rnn.LSTMBlockCell\n",
        "    batch_size: Int, the batch size to use\n",
        "    training: Boolean, whether the layer is used for training\n",
        "\n",
        "  Returns:\n",
        "    A Tensor of shape (max_sequence_length, batch_size, output_size).\n",
        "  \"\"\"\n",
        "  hidden_outputs = []\n",
        "  autograph.utils.set_element_type(hidden_outputs, tf.float32)\n",
        "  state, output = cell.zero_state(batch_size, tf.float32)\n",
        "  for ch in chars:\n",
        "    cell_output, (state, output) = cell.call(ch, (state, output))\n",
        "    hidden_outputs.append(cell_output)\n",
        "  hidden_outputs = hidden_outputs.stack()\n",
        "  if training:\n",
        "    hidden_outputs = tf.nn.dropout(hidden_outputs, 0.5)\n",
        "  return hidden_outputs\n",
        "\n",
        "\n",
        "@autograph.convert(recursive=True)\n",
        "def model(inputs, colorbot, batch_size, training):\n",
        "  \"\"\"RNNColorbot model.\n",
        "  \n",
        "  The model consists of two RNN layers (made by lower_cell and upper_cell),\n",
        "  followed by a fully connected layer with ReLU activation.\n",
        "  \n",
        "  Args:\n",
        "    inputs: A tuple (chars, length)\n",
        "    colorbot: An object of type RnnColorbot\n",
        "    batch_size: Int, the batch size to use\n",
        "    training: Boolean, whether the layer is used for training\n",
        "    \n",
        "  Returns:\n",
        "    A Tensor of shape (batch_size, 3) - the model predictions.\n",
        "  \"\"\"\n",
        "  (chars, length) = inputs\n",
        "  seq = tf.transpose(chars, [1, 0, 2])\n",
        "  seq.set_shape((None, batch_size, 256))\n",
        "\n",
        "  seq = rnn_layer(seq, colorbot.lower_cell, batch_size, training)\n",
        "  seq = rnn_layer(seq, colorbot.upper_cell, batch_size, training)\n",
        "\n",
        "  # Grab just the end-of-sequence from each output.\n",
        "  indices = tf.stack([length - 1, range(batch_size)], axis=1)\n",
        "  sequence_ends = tf.gather_nd(seq, indices)\n",
        "  return colorbot.relu_layer(sequence_ends)\n",
        "\n",
        "@autograph.convert()\n",
        "def loss_fn(labels, predictions):\n",
        "  return tf.reduce_mean((predictions - labels) ** 2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "JjK4gXFvFsf4",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "source": [
        "We will now create the model function for the estimator.\n",
        "\n",
        "In the model function, we simply call the converted functions that we defined above - that's it!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "-yso_Nx23Gy1",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "outputs": [],
      "source": [
        "def model_fn(features, labels, mode, params):\n",
        "  \"\"\"Estimator model function.\"\"\"\n",
        "  chars = features['chars']\n",
        "  sequence_length = features['sequence_length']\n",
        "  inputs = (chars, sequence_length)\n",
        "\n",
        "  # Create the model components.\n",
        "  # Simply calling the AutoGraph-ed functions and objects just works!\n",
        "  colorbot = RnnColorbot()\n",
        "  \n",
        "  batch_size = params['batch_size']\n",
        "\n",
        "  if mode == tf.estimator.ModeKeys.TRAIN:\n",
        "    predictions = model(inputs, colorbot, batch_size, training=True)\n",
        "    loss = loss_fn(labels, predictions)\n",
        "\n",
        "    learning_rate = params['learning_rate']\n",
        "    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)\n",
        "    global_step = tf.train.get_global_step()\n",
        "    train_op = optimizer.minimize(loss, global_step=global_step)\n",
        "    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)\n",
        "\n",
        "  elif mode == tf.estimator.ModeKeys.EVAL:\n",
        "    predictions = model(inputs, colorbot, batch_size, training=False)\n",
        "    loss = loss_fn(labels, predictions)\n",
        "\n",
        "    return tf.estimator.EstimatorSpec(mode, loss=loss)\n",
        "  \n",
        "  elif mode == tf.estimator.ModeKeys.PREDICT:\n",
        "    # For prediction, we expect single tensors.\n",
        "    predictions = model(inputs, colorbot, 1, training=False)\n",
        "\n",
        "    predictions = tf.minimum(predictions, 1.0)\n",
        "    return tf.estimator.EstimatorSpec(mode, predictions=predictions)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "HOQfoBnHC9CP",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "source": [
        "We'll create an input function that will feed our training and eval data."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        },
        "colab_type": "code",
        "id": "FJZlx7yG2MP0",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "outputs": [],
      "source": [
        "def input_fn(data_dir, data_url, params, training=True):\n",
        "  \"\"\"An input function for training\"\"\"\n",
        "  batch_size = params['batch_size']\n",
        "  \n",
        "  # load_dataset defined above\n",
        "  dataset = load_dataset(data_dir, data_url, batch_size, training=training)\n",
        "\n",
        "  # Package the pipeline end in a format suitable for the estimator.\n",
        "  labels, chars, sequence_length = dataset.make_one_shot_iterator().get_next()\n",
        "  features = {\n",
        "      'chars': chars,\n",
        "      'sequence_length': sequence_length\n",
        "  }\n",
        "\n",
        "  return features, labels"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "qsvv-lzbDqXd",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "source": [
        "We now have everything in place to build our custom estimator and use it for training and eval!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "height": 35
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 10064,
          "status": "ok",
          "timestamp": 1523580419240,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 240
        },
        "id": "2pg1AfbxBJQq",
        "outputId": "41894b16-3d3a-4e30-f6e4-5a9c837a2210",
        "slideshow": {
          "slide_type": "-"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Eval loss at step 100: 0.0665446\n"
          ]
        }
      ],
      "source": [
        "params = {\n",
        "    'batch_size': 64,\n",
        "    'learning_rate': 0.01,\n",
        "}\n",
        "\n",
        "train_url = \"https://raw.githubusercontent.com/random-forests/tensorflow-workshop/master/extras/colorbot/data/train.csv\"\n",
        "test_url = \"https://raw.githubusercontent.com/random-forests/tensorflow-workshop/master/extras/colorbot/data/test.csv\"\n",
        "data_dir = \"tmp/rnn/data\"\n",
        "\n",
        "regressor = tf.estimator.Estimator(\n",
        "    model_fn=model_fn,\n",
        "    params=params)\n",
        "\n",
        "regressor.train(\n",
        "    input_fn=lambda: input_fn(data_dir, train_url, params),\n",
        "    steps=100)\n",
        "eval_results = regressor.evaluate(\n",
        "    input_fn=lambda: input_fn(data_dir, test_url, params, training=False),\n",
        "    steps=2\n",
        ")\n",
        "\n",
        "print('Eval loss at step %d: %s' % (eval_results['global_step'], eval_results['loss']))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "zG1YAjB_cUnQ",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "source": [
        "And here's the same estimator used for inference."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "height": 343
        },
        "colab_type": "code",
        "executionInfo": {
          "elapsed": 31286,
          "status": "ok",
          "timestamp": 1523580450579,
          "user": {
            "displayName": "",
            "photoUrl": "",
            "userId": ""
          },
          "user_tz": 240
        },
        "id": "dxHex2tUN_10",
        "outputId": "b3dc558d-b800-4e9b-e60e-3441124e80d8",
        "slideshow": {
          "slide_type": "slide"
        }
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "\u003clink rel=stylesheet type=text/css href='/nbextensions/google.colab/tabbar.css'\u003e\u003c/link\u003e"
            ],
            "text/plain": [
              "\u003cIPython.core.display.HTML at 0x7f4112527e90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "\u003cscript src='/nbextensions/google.colab/tabbar_main.min.js'\u003e\u003c/script\u003e"
            ],
            "text/plain": [
              "\u003cIPython.core.display.HTML at 0x7f4112527f10\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "\u003cdiv id=\"id1\"\u003e\u003c/div\u003e"
            ],
            "text/plain": [
              "\u003cIPython.core.display.HTML at 0x7f4112527f50\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"2c60f474-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = colab_lib.createTabBar({\"initialSelection\": 0, \"location\": \"top\", \"contentHeight\": [\"initial\"], \"borderColor\": [\"#a7a7a7\"], \"contentBorder\": [\"0px\"], \"tabNames\": [\"RNN Colorbot\"], \"elementId\": \"id1\"});\n",
              "//# sourceURL=js_a0db480422"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd1d0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"2c60f475-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_d2a46ea291"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd0d0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"2c60f476-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_0a8262c6e9"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd390\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"2c60f477-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_e32f85ccd2"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd490\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"2c60f478-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"2c60f477-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_eaee748b21"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd550\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"2c60f479-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_2befe06587"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f4112527f10\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b1a-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"2c60f476-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_8ec4aeeb25"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd690\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b1b-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_9f9f4574f1"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd350\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b1c-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_bcccd8f300"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd6d0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b1d-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"354d7b1c-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_2c056cee72"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd490\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b1e-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_c853c3f58b"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd610\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b1f-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"354d7b1b-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_e5730ab00d"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2050\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b20-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_a897ef7e24"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2250\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b21-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_565fa3d154"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f4113124d90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b22-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"354d7b21-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_222e0dc6af"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f4113124c10\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"354d7b23-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_831db7458f"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f4113124310\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fab4-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"354d7b20-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_adb576c6eb"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f990850\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fab5-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_9418f2d32f"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f990850\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fab6-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_3fad25f306"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f4112527ed0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fab7-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3803fab6-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_45b9340e7b"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f990c90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fab8-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_bec9896d44"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f990a10\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fab9-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3803fab5-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_460b91ad4a"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3a10\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803faba-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_7dedd0b037"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3890\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fabb-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_4b1c977dc7"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3bd0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fabc-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3803fabb-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_d64fedfcf9"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3410\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3803fabd-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_3e8c929c3f"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3c50\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b986c-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3803faba-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_9f9cf2b76f"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd590\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b986d-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_b402e6b587"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3d90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b986e-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_9b7d66db72"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3b10\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b986f-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3b9b986e-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_11ec213a3f"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3950\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9870-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_9c055e4bc0"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41b21d3850\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQwAAAENCAYAAAD60Fs2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACMRJREFUeJzt3F+IlfW+x/Gvp3FECyIqU4PCO7EgZnQtnUJ0JJGoTDoY\n/dGrMBJhosggIgK7KwwiMdxRF11F/0AJvIisLBqcguxCjEAkmNQGcRvVwIzm71zsc4Yje7P3x9h7\nz97u1+tqrYdnPeu7nos3v2f9m9FaawUQ+K/pHgD49yEYQEwwgJhgADHBAGKCAcQEg2nx9NNPV7fb\nrfvuu69GRkZq5cqV0z0SAcG4xK1evbqGh4ene4wLfPXVVzU8PFyfffZZvf3221VVNWPGjGmeioRg\n8E/122+/1Q8//FDXX399zZo1a7rH4SIJxiXsqaeeqhMnTtSWLVuqv7+/Xn/99frmm2/q/vvvr06n\nU+vXr6+RkZGp/Tdt2lQvv/xyPfDAA9Xf318PP/xwnTlzpqqqJicna9u2bbVs2bLqdDq1YcOGOn36\ndFVVjY2N1ZYtW2rZsmW1du3aeuedd6aOuXPnzhoaGqpt27bV0qVL67333qtnn322Dh06VP39/bVz\n584/m/vo0aO1adOm6nQ6dffdd9f+/furqmp0dLQ6nc7Ufs8880zdeuutU/e3bdtWb7755t/3JHKh\nxiVtcHCwDQ8Pt9ZaO3nyZOt2u+3AgQOttda++OKL1u122+nTp1trrW3cuLGtWbOmff/9921iYqJt\n3Lix7dixo7XW2ltvvdUeffTRNjEx0c6fP98OHz7cfvnll9Zaaw899FDbvn17m5ycbEeOHGnLly+f\nes5XXnml3XTTTe2jjz5qrbU2MTHR3n///fbggw9OzXjw4MG2cuXK1lprZ8+ebWvWrGm7d+9uZ8+e\nbcPDw62vr68dO3Zs6vUcPny4tdba2rVr2+23396OHj3aWmtt1apV7ciRI/+oU0lrzQrjP0D7358L\n7d27t1atWlUrVqyoqqqBgYG6+eab69NPP53a9957760bbrihent764477qgjR45UVVVPT0+dOXOm\njh07VjNmzKjFixfX5ZdfXidPnqyvv/66nnzyyZo5c2YtWrSoNmzYUHv27Jk6Zl9fX61evbqqqnp7\ne//qrIcOHarx8fF65JFHqqenp5YvX16Dg4P1wQcfVFXV0qVLa2RkpE6dOlVVVWvXrq0vv/yyRkdH\n69dff61Fixb9nc4af0nPdA/AP8/x48dr37599fHHH1fVn0Jy7ty5GhgYmNrnmmuumbo9e/bsGh8f\nr6qqe+65p06ePFlPPPFE/fzzz7Vu3bp6/PHHa2xsrK688sqaPXv21OMWLFhQhw8fnro/b968eMax\nsbGaP3/+BdsWLFhQY2NjVVXV6XRq//79dd1111W3261ut1t79uyp3t7eWrJkyUWcDX4PwbjE/f9P\nH+bPn1/r16+v7du3X/Rxenp6auvWrbV169Y6fvx4bd68uRYuXFi33XZb/fTTTzU+Pl5z5sypqqoT\nJ07U3Llz/+IMf8vcuXPrxIkTF2w7fvx4LVy4sKqqut1uvfjiizV//vzqdDrV399fzz33XPX29la3\n273o18XFcUlyibv22mtrdHS0qqrWrVtX+/fvr88//7zOnz9fExMTNTIyUj/++OPfPM7Bgwfru+++\nq/Pnz9ecOXOqp6enLrvsspo3b1719fXVSy+9VJOTk/Xtt9/Wu+++W+vWrftd895yyy01Z86ceu21\n1+rcuXN18ODB+uSTT+rOO++sqqobb7yxZs2aVXv37q1Op1NXXHFFXX311fXhhx9e8IYo/xiCcYnb\nvHlz7dq1q7rdbu3bt6927dpVu3fvroGBgRocHKw33nhj6j2Ov7YSOHXqVA0NDdWSJUvqrrvuqmXL\nlk1FYceOHTU6OlorVqyooaGheuyxxy64zLkYM2fOrFdffbUOHDhQy5cvr+eff75eeOGFqRVG1Z9W\nGVddddXUpc7/hWLx4sW/6znJzWjNH+gAGSsMICYYQEwwgJhgALF/2e9h/PEP/z3dI8B/tKseee/P\ntllhADHBAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHEBAOICQYQEwwgJhhATDCAmGAAMcEA\nYoIBxAQDiAkGEBMMICYYQEwwgJhgADHBAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHEBAOI\nCQYQEwwgJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEwwgJhgADHBAGKCAcQEA4gJBhATDCAm\nGEBMMICYYAAxwQBiggHEBAOICQYQEwwgJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEwwgJhg\nADHBAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHEBAOICQYQEwwgJhhATDCAmGAAMcEAYoIB\nxAQDiAkGEBMMICYYQEwwgJhgADHBAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHEBAOICQYQ\nEwwgJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEwwgJhgADHBAGKCAcQEA4gJBhATDCAmGEBM\nMICYYAAxwQBiggHEBAOICQYQEwwgJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEwwgJhgADHB\nAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHEBAOICQYQEwwgJhhATDCAmGAAMcEAYoIBxAQD\niAkGEBMMICYYQEwwgJhgADHBAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHEBAOICQYQEwwg\nJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEwwgJhgADHBAGKCAcQEA4gJBhATDCAmGEBMMICY\nYAAxwQBiggHEBAOICQYQEwwgJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEwwgJhgADHBAGKC\nAcQEA4gJBhATDCA2o7XWpnsI4N+DFQYQEwwgJhhATDCAmGAAMcEAYoIBxAQDiAkGEBMMICYYQEww\ngJhgADHBAGKCAcQEA4gJBhATDCAmGEBMMICYYAAxwQBiggHE/gfh60wGjfc7LQAAAABJRU5ErkJg\ngg==\n",
            "text/plain": [
              "\u003cmatplotlib.figure.Figure at 0x7f4113124310\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9871-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3b9b986d-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_ba6a061307"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd890\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9872-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.getActiveOutputArea();\n",
              "//# sourceURL=js_83e3496927"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd590\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9873-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = document.querySelector(\"#id1_content_0\");\n",
              "//# sourceURL=js_f437bab20d"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a22d0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9874-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3b9b9873-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_93aa63450e"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2b90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9875-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"id1\"].setSelectedTabIndex(0);\n",
              "//# sourceURL=js_aca189bea5"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd4d0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "\u003cdiv class=id_100313201 style=\"margin-right:10px; display:flex;align-items:center;\"\u003e\u003cspan style=\"margin-right: 3px;\"\u003e\u003c/span\u003e\u003c/div\u003e"
            ],
            "text/plain": [
              "\u003cIPython.core.display.HTML at 0x7f410f990a90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9876-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = jQuery(\".id_100313201 span\");\n",
              "//# sourceURL=js_5df1fe383e"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f410f8fd490\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3b9b9877-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"3b9b9876-3eb4-11e8-91ec-c8d3ffb5fbe0\"].text(\"Give me a color name (or press 'enter' to exit): \");\n",
              "//# sourceURL=js_c62c7174ad"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2390\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3ed76584-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = jQuery(\".id_100313201 input\");\n",
              "//# sourceURL=js_2e2201ddc4"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2810\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3ed76585-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"3ed76584-3eb4-11e8-91ec-c8d3ffb5fbe0\"].remove();\n",
              "//# sourceURL=js_288e5283d6"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a26d0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3ed76586-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = jQuery(\".id_100313201 span\");\n",
              "//# sourceURL=js_2f31d19cde"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2fd0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3ed76587-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = window[\"3ed76586-3eb4-11e8-91ec-c8d3ffb5fbe0\"].text(\"Give me a color name (or press 'enter' to exit): \");\n",
              "//# sourceURL=js_2fbbcda050"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f4112527e90\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1",
              "user_output"
            ]
          },
          "output_type": "display_data"
        },
        {
          "data": {
            "application/javascript": [
              "window[\"3ed76588-3eb4-11e8-91ec-c8d3ffb5fbe0\"] = google.colab.output.setActiveOutputArea(window[\"3b9b9872-3eb4-11e8-91ec-c8d3ffb5fbe0\"]);\n",
              "//# sourceURL=js_f94d975cf3"
            ],
            "text/plain": [
              "\u003cIPython.core.display.Javascript at 0x7f41127a2fd0\u003e"
            ]
          },
          "metadata": {
            "tags": [
              "id1_content_0",
              "outputarea_id1"
            ]
          },
          "output_type": "display_data"
        }
      ],
      "source": [
        "def predict_input_fn(color_name):\n",
        "  \"\"\"An input function for prediction.\"\"\"\n",
        "  _, chars, sequence_length = parse(color_name)\n",
        "  \n",
        "  # We create a batch of a single element.\n",
        "  features = {\n",
        "      'chars': tf.expand_dims(chars, 0),\n",
        "      'sequence_length': tf.expand_dims(sequence_length, 0)\n",
        "  }\n",
        "  return features, None\n",
        "\n",
        "\n",
        "def draw_prediction(color_name, pred):\n",
        "  pred = pred * 255\n",
        "  pred = pred.astype(np.uint8)\n",
        "  plt.axis('off')\n",
        "  plt.imshow(pred)\n",
        "  plt.title(color_name)\n",
        "  plt.show()\n",
        "\n",
        "\n",
        "def predict_with_estimator(color_name, regressor):\n",
        "  predictions = regressor.predict(\n",
        "      input_fn=lambda:predict_input_fn(color_name))\n",
        "  pred = next(predictions)\n",
        "  predictions.close()\n",
        "  pred = np.minimum(pred, 1.0)\n",
        "  pred = np.expand_dims(np.expand_dims(pred, 0), 0)\n",
        "\n",
        "  draw_prediction(color_name, pred)\n",
        "\n",
        "tb = widgets.TabBar([\"RNN Colorbot\"])\n",
        "while True:\n",
        "  with tb.output_to(0):\n",
        "    try:\n",
        "      color_name = six.moves.input(\"Give me a color name (or press 'enter' to exit): \")\n",
        "    except (EOFError, KeyboardInterrupt):\n",
        "      break\n",
        "  if not color_name:\n",
        "    break\n",
        "  with tb.output_to(0):\n",
        "    tb.clear_tab()\n",
        "    predict_with_estimator(color_name, regressor)\n",
        "  "
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "default_view": {},
      "name": "RNN Colorbot using Estimators",
      "provenance": [
        {
          "file_id": "1CtzefX39ffFibX_BqE6cRbT0UW_DdVKl",
          "timestamp": 1523579810961
        },
        {
          "file_id": "1DcfimonWU11tmyivKBGVrbpAl3BIOaRG",
          "timestamp": 1523016192637
        },
        {
          "file_id": "1wCZUh73zTNs1jzzYjqoxMIdaBWCdKJ2K",
          "timestamp": 1522238054357
        },
        {
          "file_id": "1_HpC-RrmIv4lNaqeoslUeWaX8zH5IXaJ",
          "timestamp": 1521743157199
        },
        {
          "file_id": "1mjO2fQ2F9hxpAzw2mnrrUkcgfb7xSGW-",
          "timestamp": 1520522344607
        }
      ],
      "version": "0.3.2",
      "views": {}
    },
    "kernelspec": {
      "display_name": "Python 2",
      "name": "python2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
